package com.cn.dim.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.dim.pojo.Auth;
import com.cn.dim.pojo.User;
import com.cn.dim.service.AuthService;
import com.cn.dim.utils.MenuTreeBuilder;
import com.github.pagehelper.Page;


@Controller
@RequestMapping("/auth")
public class AuthController {

	@Resource
	private AuthService authService;

	@RequestMapping(value ="/queryAuth",method = RequestMethod.GET)
	public String queryAuth(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> param=new HashMap<>();
		try {
			List<Auth> rList = authService.queryAuth(param);
			List<Auth> nodes = rList;
			String json  = new MenuTreeBuilder().buildTree(nodes);
			
			
			response.setContentType("text/html;charset=utf-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(json);
			response.getWriter().flush();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping(value ="/queryAuthByUser",method = RequestMethod.GET)
	public String queryAuthByUser(HttpServletRequest request,HttpServletResponse response){
		Map<String,Object> param=new HashMap<>();
		
		User cUser=(User)request.getSession().getAttribute("user");//从session中获取当前登录用户
		param.put("userid", cUser.getId());
		try {
			List<Auth> rList = authService.queryAuthByUser(param);
			List<Auth> nodes = rList;
			String json  = new MenuTreeBuilder().buildTree(nodes);
			response.setContentType("text/html;charset=utf-8");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setDateHeader("Expires", 0);
			response.getWriter().write(json);
			response.getWriter().flush();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return null;
	}
}
